/****************************************************************************
*
* Copyright(c) 2022  C*Core -   All Rights Reserved
*
* THIS SOFTWARE IS DISTRIBUTED "AS IS, " AND ALL WARRANTIES ARE DISCLAIMED,
* INCLUDING MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* PROJECT     : CCFC2011BC
* DESCRIPTION : CCFC2011BC INTC low level drivers Code
* HISTORY     : Initial version
* @file     intc_lld.c
* @version  1.1
* @date     2023 - 02 - 20
* @brief    Initial version.
*
*****************************************************************************/
#include "intc_lld.h"
#include "CCFC2011BC.h"
#include "e200z3_asm.h"

/************************************************************************
 *    CCFC2011BC: IVOR4 < ->Interrupt Controller(INTC)
 ************************************************************************/

/*******************************************************************************
 * @brief      Interrupt initialise.
 * @param[in]  IRQHandler: Interrupt handler function.
 *             IRQn: IRQn_t
 *             psrPri: INTC_PSR_PRI_t
 *             To enable interruption, the psrPri must be > 0,
 *             because the psrPri must be > cprPri to enable.
 * @param[out] None
 * @retval     None
 * @notapi 
 *******************************************************************************/
void INTC_SoftInit(INTCInterruptFn IRQHandler, uint16_t IRQn, uint8_t psrPri)
{
    INTC_MCR_SET((uint32_t)MCR_HVEN_SOFTWARE|(uint32_t)MCR_VTES_4BYTES);
    INTC_InstallINTCInterruptHandler(IRQHandler, IRQn, psrPri);
}

/*******************************************************************************
 * @brief      INTC Interrupt enable.
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi 
 *******************************************************************************/
void INTC_IRQ_Enable(void)
{
    /* Interruption is enabled only when INTC.PSR > INTC.CPR.
     * Therefore, to enable interruption,
     * the current priority must be set to the[minimum]. */
	INTC.CPR.B.PRI = (uint32_t)CPR_PRI_0;  /* Set the current priority */
}

/*******************************************************************************
 * @brief      INTC Interrupt disable.
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi 
 *******************************************************************************/
void INTC_IRQ_Disable(void)
{
    /* Interruption is enabled only when INTC.PSR > INTC.CPR.
     * Therefore, to disable interruption,
     * you need to set the current priority to the[maximum]. */
	INTC.CPR.B.PRI = (uint32_t)CPR_PRI_15; /* Set the current priority */
}

/*******************************************************************************
 * @brief      INTC Interrupt Init.
 * @param[in]  cprPriority: INTC_CPR_t
 * @param[out] None
 * @retval     None
 * @notapi 
 *******************************************************************************/
void INTC_CPR_Init(uint8_t cprPriority)
{
    INTC.CPR.B.PRI = cprPriority;
}

/*******************************************************************************
 * @brief      INTC Software Set/Clear Interrupt Registers
 *             SETx:
 *                  Writing a 1 sets the corresponding CLRx bit.
 *                  Writing a 0 has no effect.
 *                  Each SETx always will be read as a 0.
 *             CLRx: is the flag bit.
 *                  Writing a 1 to CLRx clears it provided that
 *                      a 1 is not written simultaneously to
 *                      its corresponding SETx bit.
 *                  Writing a 0 to CLRx has no effect.
 * @param[in]  sscirNum: 0U~7U
 * @param[out] None
 * @retval     None
 * @notapi 
 *******************************************************************************/
void INTC_SSCIR_ClearFlagBits(uint8_t sscirNum)
{
    INTC.SSCIR[sscirNum].R = (uint32_t)INTC_SSCIR_SET|(uint32_t)INTC_SSCIR_CLR;
}

/*******************************************************************************
 * @brief      irqSysHalt
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi 
 *******************************************************************************/
void irqSysHalt(void)
{
    irqIsrDisable(); /* PRQA S 1006 */
    for (; ;)
    {
        ;
    }
}
